import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import java.awt.TextField;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class TheView{

	private Thread blueT;
	private Thread redT;
	private Thread dragonT;
	private Thread baronT;
	private JButton blueButton, redButton, dragonButton, baronButton;
	

	public TheView() {
		initiateGUI();

	}
	public void initiateGUI() {
		JFrame jf = new JFrame(); 			//Initiate JFrame
		jf.setSize(new Dimension(400, 200));
		jf.setVisible(true);
		jf.setBackground(Color.GRAY);
		jf.setLocationRelativeTo(null);
		jf.setResizable(false);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Initiate Blue
		JPanel panel = new JPanel();
		panel.setLayout(null);
		final TextField blue = new TextField();
		blue.setBounds(56, 5, 50, 33);
		blue.setText("5:00");
		blue.setFont(new Font("SansSerif", Font.BOLD, 20));
		blue.setEditable(false);
		blue.setBackground(Color.green);
		panel.add(blue);

		//Initiate Red
		final TextField red = new TextField();
		red.setBounds(125, 5, 50, 33);
		red.setText("5:00");
		red.setFont(new Font("SansSerif", Font.BOLD, 20));
		red.setEditable(false);
		red.setBackground(Color.green);
		panel.add(red);

		//Initiate Dragon
		final TextField dragon = new TextField();
		dragon.setBounds(208, 5, 50, 33);
		dragon.setText("6:00");
		dragon.setFont(new Font("SansSerif", Font.BOLD, 20));
		dragon.setEditable(false);
		dragon.setBackground(Color.green);
		panel.add(dragon);

		//Initiate Baron
		final TextField baron = new TextField();
		baron.setBounds(277, 5, 50, 33);
		baron.setText("7:00");
		baron.setEditable(false);
		baron.setFont(new Font("SansSerif", Font.BOLD, 20));
		baron.setBackground(Color.green);
		panel.add(baron);


		jf.getContentPane().add(panel);

		blueButton = new JButton("");

		//What happens when blue button is pressed
		blueButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if(blueT == null)  {
					blueT = new Thread(new Runnable() {

						@Override
						public void run() {
							blue.setText("5:00");
							blue.setBackground(Color.red);
							for(int i = 300; i > 0; i--)
							{
								CountDown cd = new CountDown(i);
								blue.setText(cd.getCurrentTime());
							}
							blue.setText("5:00");
							blue.setBackground(Color.GREEN);
						}}); 
					blueT.start();
				}
				else {
					blueT.stop();
					blue.setText("5:00");
					blue.setBackground(Color.green);
					blueT = null;
				}
			}

		});
		blueButton.setIcon(new ImageIcon("images/blue.png"));
		blueButton.setBounds(56, 57, 50, 50);
		panel.add(blueButton);

		redButton = new JButton("");

		//What happens when red button is pressed
		redButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if(redT == null)  {
					redT = new Thread(new Runnable() {

						@Override
						public void run() {
							red.setText("5:00");
							red.setBackground(Color.RED);
							for(int i = 300; i > 0; i--)
							{
								CountDown cd = new CountDown(i);
								red.setText(cd.getCurrentTime());
							}
							red.setBackground(Color.green);

						}}); 
					redT.start();
				}
				else {
					redT.stop();
					red.setText("5:00");
					red.setBackground(Color.green);
					redT = null;
				}
			}

		});


		redButton.setBounds(125, 57, 50, 50);
		redButton.setIcon(new ImageIcon("images/red.png"));
		panel.add(redButton);

		dragonButton = new JButton("");

		//What happens when dragon button is pressed
		dragonButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if(dragonT == null)  {
					dragonT = new Thread(new Runnable() {

						@Override
						public void run() {
							dragon.setText("6:00");
							dragon.setBackground(Color.RED);
							for(int i = 360; i > 0; i--)
							{
								CountDown cd = new CountDown(i);
								dragon.setText(cd.getCurrentTime());
							}
							dragon.setBackground(Color.green);
						}}); 
					dragonT.start();
				}
				else {
					dragonT.stop();
					dragon.setText("6:00");
					dragon.setBackground(Color.green);
					dragonT = null;
				}
			}

		});
		dragonButton.setBounds(208, 57, 50, 50);
		dragonButton.setIcon(new ImageIcon("images/dragon.png"));
		panel.add(dragonButton);

		baronButton = new JButton("");

		//What happens when baron button is pressed
		baronButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if(baronT == null)  {
					baronT = new Thread(new Runnable() {

						@Override
						public void run() {
							baron.setText("7:00");
							baron.setBackground(Color.RED);
							for(int i = 420; i > 0; i--)
							{
								CountDown cd = new CountDown(i);
								baron.setText(cd.getCurrentTime());
							}
							baron.setBackground(Color.green);
						}}); 
					baronT.start();
				}
				else {
					baronT.stop();
					baron.setText("7:00");
					baron.setBackground(Color.green);
					baronT = null;
				}
			}

		});
		baronButton.setBounds(277, 57, 50, 50);
		baronButton.setIcon(new ImageIcon("images/baron.png"));
		panel.add(baronButton);

		JButton btnNewButton = new JButton("Exit");
		btnNewButton.setToolTipText("Click here to exit");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {		//WHat happens when u press exit

				System.exit(0);

			}
		});
		btnNewButton.setBounds(146, 127, 97, 25);
		panel.add(btnNewButton);

		panel.repaint();
	}

	public void clickBlue() {
		blueButton.doClick();
	}
	public void clickRed() {
		redButton.doClick();
		
	}
	public void clickDragon() {
		dragonButton.doClick();
	}
	public void clickBaron() {
		baronButton.doClick();
	}

}
